package com.example.myapplication.base

import android.os.Bundle
import android.os.PersistableBundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

/**
 * @Description
 * @Author junhao.ruan@tcl.com
 * @Date 2021/7/21
 */
abstract class BaseFragment<T : ViewBinding> :Fragment(){

    lateinit var binding:T

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return super.onCreateView(inflater, container, savedInstanceState)
        val type = javaClass.genericSuperclass
        if(type is ParameterizedType){
            val clazz = type.actualTypeArguments[0] as Class<T>
            val method = clazz.getMethod("inflate",LayoutInflater::class.java)
            binding = method.invoke(null,layoutInflater) as T
        }
        return binding.root;

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
    }

    abstract fun initData()
}